﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Kaskelot.SP.UI.Analys.ViewModel;
using Kaskelot.SP.UI.Analys.Data;
using Kaskelot.SP.UI.Analys.Domain;
using Telerik.Windows.Controls;
using Kaskelot.SP.UI.Analys.Data.Model;
using System.Runtime.Serialization;

namespace Kaskelot.SP.UI.Analys
{
	public partial class MainPage : UserControl
	{
		private Mode currentMode;

		public MainPage()
		{
			InitializeComponent();
			LocalizationManager.Manager = new CustomLocalizationManager();
			Loaded += new RoutedEventHandler(MainPage_Loaded);
			ErrorHandler.Instance.OnError += new EventHandler<GenericEventArgs<Exception>>(Instance_OnError);
			ErrorHandler.Instance.OnResetError += new EventHandler<GenericEventArgs<object>>(Instance_OnResetError);
		}

		void Instance_OnResetError(object sender, GenericEventArgs<object> e)
		{
			ErrorRow.Height = new GridLength(0.0);
		}

		void Instance_OnError(object sender, GenericEventArgs<Exception> e)
		{
			if (e.Item is InvalidDataContractException)
			{
				ErrorText.Text = "Ogiltigt filter.";
			}
			else
			{
				ErrorText.Text = e.Item.Message;
			}

			ErrorRow.Height = new GridLength(100.0);
		}

		private void MainPage_Loaded(object sender, RoutedEventArgs e)
		{
			GoToState(Mode.GridWithQuery);
			QueryBox.FilterReady += new EventHandler<GenericEventArgs<Query>>(QueryBox_FilterReady);
			QueryBox.FieldsUpdated += new EventHandler<GenericEventArgs<List<Field>>>(QueryBox_FieldsUpdated);

			//AnalyseServiceWrapper.Instance.GetAnalyseInfoLinkUrl(
			//	(string url) =>
			//	{
			//		HelpButton.NavigateUri = new Uri(url);
			//	},
			//	(Exception ex) =>
			//	{
			//		ErrorHandler.Instance.HandleError(ex);
			//	});
		}

		void QueryBox_FieldsUpdated(object sender, GenericEventArgs<List<Field>> e)
		{
			GridBox.SetFields(e.Item);
		}

		private void QueryBox_FilterReady(object sender, GenericEventArgs<Query> e)
		{
			if (e.Item == null)
			{
				GridBox.SetData(null);
				return;
			}

			AnalyseServiceWrapper.Instance.ExecuteQuery(e.Item,
				(QueryResult result) =>
				{
					GridBox.SetData(result.Data);
					GoToState(Mode.GridWithQuery);
				},
				(Exception ex) =>
				{
					ErrorHandler.Instance.HandleError(ex);
				});
		}

		private void ShowQueryButton_Click(object sender, RoutedEventArgs e)
		{
			if (currentMode == Mode.Grid)
			{
				GoToState(Mode.GridWithQuery);
				GridBox.SetHeight(335);
			}
			else
			{
				GoToState(Mode.Grid);
				GridBox.SetHeight(720);
			}
		}

		private void GoToState(Mode newMode)
		{
			currentMode = newMode;
			switch (currentMode)
			{
				case Mode.Grid:
					QueryBox.Visibility = System.Windows.Visibility.Collapsed;
					GridBox.Visibility = System.Windows.Visibility.Visible;

					QueryToggle.Content = "Visa urval ›";
					break;
				case Mode.GridWithQuery:
					QueryBox.Visibility = System.Windows.Visibility.Visible;
					GridBox.Visibility = System.Windows.Visibility.Visible;
					QueryToggle.Content = "Göm urval ›";
					break;
				default:
					break;
			}
		}

		private enum Mode
		{
			Grid,
			GridWithQuery
		}
	}
}
